﻿using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestThreads
{
    class Program
    {
        static void Main(string[] args)
        {
            
            log(GetThreadCount().ToString());
            Thread t = new Thread(Hello);
            t.Start();
            Thread.Sleep(1000);
            t.Join();
           
            
        }

        private static int GetThreadCount()
        {

            //return ((IEnumerable)System.Diagnostics.Process.GetCurrentProcess().Threads).OfType<System.Diagnostics.ProcessThread>().Where(t => t.ThreadState == System.Diagnostics.ThreadState.Running).Count();
            return System.Diagnostics.Process.GetCurrentProcess().Threads.Count;
        }

        public static void Hello()
        {
            log(GetThreadCount().ToString());
            Thread.Sleep(2000);
        }

        public static void log(string info)
        {
            // Formatter
            TextFormatter briefFormatter = new TextFormatter(info);

            // Trace Listener
            var flatFileTraceListener = new FlatFileTraceListener(
              @"C:\Temp\FlatFile.log",
              "----------------------------------------",
              "----------------------------------------",
              briefFormatter);

            // Build Configuration
            var config = new LoggingConfiguration();

            config.AddLogSource("DiskFiles", SourceLevels.All, true)
              .AddTraceListener(flatFileTraceListener);

            LogWriter defaultWriter = new LogWriter(config);

            // Check if logging is enabled before creating log entries.
            if (defaultWriter.IsLoggingEnabled())
            {
                defaultWriter.Write("Log entry created using the simplest overload.");
                defaultWriter.Write("Log entry with a single category.", "General");
                defaultWriter.Write("Log entry with a category, priority, and event ID.",
                                    "General", 6, 9001);
                defaultWriter.Write("Log entry with a category, priority, event ID, "
                                    + "and severity.", "General", 5, 9002,
                                    TraceEventType.Warning);
                defaultWriter.Write("Log entry with a category, priority, event ID, "
                                    + "severity, and title.", "General", 8, 9003,
                                    TraceEventType.Warning, "Logging Block Examples");
            }
            else
            {
                Console.WriteLine("Logging is disabled in the configuration.");
            }

        }

    }
}
